package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.Utils.Page;
import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Article;
import com.example.shoppingmall.service.ArticleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ArticleType")
public class ArticleTypeController {
    @Autowired
    ArticleTypeService articleTypeService;

    @RequestMapping("/ArticleTypelist")
    public String ArticleTypelist(@RequestParam(value = "pageno",defaultValue = "1") String pageno,
                                  @RequestParam(value = "pageSize",defaultValue = "6") String pageSize,
                                  Model model){
        int count=articleTypeService.articlecount();
        int num=(Integer.valueOf(pageno)-1)*Integer.valueOf(pageSize);
        Page page=new Page();
        page.setPageno(Integer.valueOf(pageno));
        page.setPageCount(count);
        List<Article> articleList=articleTypeService.getarticlelist(num,Integer.valueOf(pageSize));
        model.addAttribute("art",articleList);
        model.addAttribute("page",page);
        return "Operate/ArticleType.html";
    }

    //新增分类
    @MyLog(value = "运营管理->文章分类->添加")
    @RequestMapping("/addarticle")
    public String addarticle(@RequestParam("articlename") String articlename){
        if(articleTypeService.AddArticle(articlename)>0){
            return "redirect:/ArticleType/ArticleTypelist";
        }
        return "error";
    }

    //查看详情
    @MyLog(value = "运营管理->文章分类->查看详情")
    @RequestMapping("/selarticle")
    @ResponseBody
    public Map<Object,Object> selarticle(@RequestParam("id") int id){
        Map<Object,Object> map=new HashMap<>();
        Article article=articleTypeService.selArticle(id);
        map.put("articles",article);
        return map;
    }

    //修改分类名称
    @MyLog(value = "运营管理->文章分类->修改")
    @RequestMapping("/Updarticle")
    public String Updarticle(@RequestParam("articlename") String articlename,@RequestParam("id") int id){
        Article article=new Article();
        article.setArticlename(articlename);
        article.setId(id);
        if(articleTypeService.UpdArticle(article)>0){
            return "redirect:/ArticleType/ArticleTypelist";
        }
        return "error";
    }

    //删除分类
    @MyLog(value = "运营管理->文章分类->删除")
    @RequestMapping("/delarticle")
    @ResponseBody
    public Map<Object,Object> delarticle(@RequestParam("id") int id){
        Map<Object,Object> map=new HashMap<>();
        if(articleTypeService.DelArticle(id)>0){
            map.put("delajax","yes");
        }else{
            map.put("delajax","no");
        }
        return map;
    }
}
